package com.wanli.agent.homepage;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.wanli.agent.R;
import com.wanli.agent.base.Adapter_ViewPager_Fragment;
import com.wanli.agent.base.BaseActivity;
import com.wanli.agent.base.BaseFragment;
import com.wanli.agent.bean.StoreDeatilsBean;
import com.wanli.agent.event.StoreDataEvent;
import com.wanli.agent.event.StoreNextEvent;
import com.wanli.agent.widget.AppTitleBar;
import com.wanli.agent.widget.NoScrollViewPager;
import com.wanli.agent.widget.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 门店资料
 */
public class StoreDataActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.viewPager)
    NoScrollViewPager viewPager;
    private List<String> mDataList;
    private Adapter_ViewPager_Fragment adapter_viewPager;
    private String type;
    private int quick_income;
    private StoreDeatilsBean.DataBean storeBean;
    private String merchant_id;
    private StoreDataEvent dataEvent;
    private String isShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_data);
        ButterKnife.bind(this);
        registerEventBus();
        initView();
    }

    public void initView() {
        type = getIntent().getStringExtra("type");
        isShow = getIntent().getStringExtra("isShow");
        quick_income = getIntent().getIntExtra("quick_income", 0);
        merchant_id = getIntent().getStringExtra("merchant_id");
        if (!TextUtils.isEmpty(merchant_id)) {
            type = "1";
            titleBar.setTitle("新增分店");
        } else {
            titleBar.setTitle("门店资料");
        }
        dataEvent = EventBus.getDefault().getStickyEvent(StoreDataEvent.class);

        if (dataEvent != null) {
            storeBean = dataEvent.getDataBean();
        }
        if (storeBean != null) {
            if (storeBean.getMerchant_type() == 0) {
                type = "1";
                quick_income = storeBean.getQuick_income();
            } else if (storeBean.getMerchant_type() == 1) {
                type = "2";
            } else if (storeBean.getMerchant_type() == 3) {
                type = "3";
            }
        }
        mDataList = new ArrayList<>();
        mDataList.add("基本信息");
        mDataList.add("公司信息");
        mDataList.add("门店信息");
        mDataList.add("上传资料");
        //ViewPager的适配器
        List<BaseFragment> fragmentList = new ArrayList<>();

        StoreBasicFragment storeBasicFragment = new StoreBasicFragment();
        Bundle arguments1 = new Bundle();
        arguments1.putString(StoreBasicFragment.TITLE_TYPE, type);
        arguments1.putString(StoreBasicFragment.MERCHANT_ID, merchant_id);
        arguments1.putInt(StoreBasicFragment.QUICK_INCOME, quick_income);
        storeBasicFragment.setArguments(arguments1);
        fragmentList.add(storeBasicFragment);

        StoreCompanyFragment storeCompanyFragment = new StoreCompanyFragment();
        Bundle arguments2 = new Bundle();
        arguments2.putString(StoreCompanyFragment.TITLE_TYPE, type);
        arguments2.putString(StoreCompanyFragment.MERCHANT_ID, merchant_id);
        arguments2.putInt(StoreCompanyFragment.QUICK_INCOME, quick_income);
        storeCompanyFragment.setArguments(arguments2);
        fragmentList.add(storeCompanyFragment);

        StoreInfoFragment storeInfoFragment = new StoreInfoFragment();
        Bundle arguments3 = new Bundle();
        arguments3.putString(StoreInfoFragment.TITLE_TYPE, type);
        arguments3.putString(StoreInfoFragment.MERCHANT_ID, merchant_id);
        arguments3.putInt(StoreInfoFragment.QUICK_INCOME, quick_income);
        storeInfoFragment.setArguments(arguments3);
        fragmentList.add(storeInfoFragment);


        StoreUpDataFragment storeUpDataFragment = new StoreUpDataFragment();
        Bundle arguments4 = new Bundle();
        arguments4.putString(StoreUpDataFragment.TITLE_TYPE, type);
        arguments4.putString(StoreUpDataFragment.IS_SHOW, isShow);
        arguments4.putString(StoreUpDataFragment.MERCHANT_ID, merchant_id);
        arguments4.putInt(StoreUpDataFragment.QUICK_INCOME, quick_income);
        storeUpDataFragment.setArguments(arguments4);
        fragmentList.add(storeUpDataFragment);

        adapter_viewPager = new Adapter_ViewPager_Fragment(getSupportFragmentManager(), fragmentList, mDataList);
        viewPager.setAdapter(adapter_viewPager);
        //指示器适配
        initMagicIndicator();
    }

    private void initMagicIndicator() {
        magicIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator = new CommonNavigator(mActivity);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setTextSize(14);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.text_color_2));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.theme_color));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 3));
                indicator.setLineWidth(UIUtil.dip2px(context, 60));
                indicator.setRoundRadius(UIUtil.dip2px(context, 2));
                indicator.setColors(getResources().getColor(R.color.theme_color));
                return indicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
        viewPager.setOffscreenPageLimit(4);
    }
    //接收广播
    @Subscribe
    public void onStoreNextEvent(StoreNextEvent event) {
        viewPager.setCurrentItem(event.getNextStep());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dataEvent != null) {
            EventBus.getDefault().removeStickyEvent(dataEvent);
        }
    }
}
